package com.jn.touch.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.jn.touch.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class SignatureView extends View {
    public static final String TAG = "asdf";
    private Path mPath = new Path();
    private Paint mPaint = new Paint();
    private Path mPath2 = new Path();
    //位图的绘制内容输出者：画布
    private Canvas mCanvas;
    //用来存放绘制内容的位图
    private Bitmap mCacheBitmap;
    //上下文
    private Context mContext;
    private float x1, x2, y1, y2;

    public SignatureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(getResources().getColor(R.color.red, null));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCacheBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mCacheBitmap);
        mCanvas.drawColor(Color.WHITE);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "第1个手指按下");
                x1 = event.getX(0);
                y1 = event.getY(0);
                mPath.moveTo(x1, y1);
                break;
            case MotionEvent.ACTION_UP:
                Log.e(TAG, "最后1个手指抬起");
                break;
            case MotionEvent.ACTION_MOVE:
                if (event.getPointerCount() >= 2) {
                    x2 = event.getX(1);
                    y2 = event.getY(1);
                    mPath2.lineTo(x2, y2);

                    x1 = event.getX(0);
                    y1 = event.getY(0);
                    mPath.lineTo(x1, y1);
                    Log.d(TAG, "======pointer=====move" + "(" + x1 + "," + y1 + "),(" + x2 + "," + y2+")") ;
                } else if (event.getPointerCount() == 1) {
                    x1 = event.getX(0);
                    y1 = event.getY(0);
                    mPath.lineTo(x1, y1);
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                Log.e(TAG,"第"+(index+1)+"个手指按下");
                x2 = event.getX(1);
                y2 = event.getY(1);
                mPath2.moveTo(x2, y2);
                break;
            case MotionEvent.ACTION_POINTER_UP:
                Log.e(TAG,"第"+(index+1)+"个手指抬起");
                break;
        }
        mCanvas.drawPath(mPath, mPaint);
        mCanvas.drawPath(mPath2, mPaint);
        invalidate();
        return true;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e(TAG, "onDraw canvas = " + canvas);
        canvas.drawPath(mPath, mPaint);
        canvas.drawPath(mPath2, mPaint);
    }

    /**
     * 清空
     */
    public void clear() {
        Log.e(TAG, "clear()");
        if (mPath != null) {
            Log.e(TAG, "before clear mPath isEmpty => " + mPath.isEmpty());
            mPath.reset();
            mCanvas.drawColor(Color.WHITE);
            Log.e(TAG, "after clear mPath isEmpty => " + mPath.isEmpty());
            invalidate();
        }
        if (mPath2 != null) {
            Log.e(TAG, "before clear mPath isEmpty => " + mPath2.isEmpty());
            mPath2.reset();
            mCanvas.drawColor(Color.WHITE);
            Log.e(TAG, "after clear mPath isEmpty => " + mPath2.isEmpty());
            invalidate();
        }
    }

    public void save() {
        //创建一个文件用于存放图片
        File file = new File(mContext.getExternalFilesDir(null) + "testSign.png");
        if (file.exists()) {
            file.delete();
        }
        OutputStream outputStream = null;
        try {
            //输出到这个文件
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(mContext, "保存异常：" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
        //压缩形成输出流
        mCacheBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        Toast.makeText(mContext, "保存成功!path:" + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
    }

    public void saveImageToGallery() {
        String savedImageURL = MediaStore.Images.Media.insertImage(
                mContext.getContentResolver(),
                mCacheBitmap,
                "testSign",
                "testSign save"
        );
        Toast.makeText(mContext, "保存成功! URL:" +
                savedImageURL, Toast.LENGTH_SHORT).show();
    }
}